package com.txy.strmatch.trie;

import com.txy.binarytree.TreeNode;

public class Trie {
    TrieNode  root = new TrieNode('/');

    public void insert(char[] txt) {
        TrieNode p = root;
        for(int i = 0; i <txt.length;i++) {
            int idx =txt[i] - 97;
            if(p.children[idx] == null) {
                TrieNode node = new TrieNode(txt[i]);
                p.children[idx] = node;
            }
            p = p.children[idx];
        }
        p.isLeaf = true;
    }

    public boolean find(char[] pattern) {
        TrieNode p = root;
        for(int i = 0;i<pattern.length;i++) {
            int idx = pattern[i] - 97;
            if(p.children[idx]==null) return false;
            p = p.children[idx];
        }
        if(p.isLeaf)
            return true;
        return false;
    }

    public static void main(String[] args) {
        Trie trie = new Trie();
        trie.insert("hello".toCharArray());
        trie.insert("he".toCharArray());
        trie.insert("el".toCharArray());
        System.out.println(trie.find("el".toCharArray()));
        System.out.println(trie.find("ele".toCharArray()));
    }
}
